package com.yiren.manager.ctrl.test;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fasterxml.jackson.core.type.TypeReference;
import com.yiren.core.exception.MyException;
import com.yiren.core.exception.SwallowException;
import com.yiren.core.repository.utils.QueryResult;
import com.yiren.core.utils.FavoriteSet;
import com.yiren.core.utils.HTTPResult;
import com.yiren.core.utils.JsonMapperTool;
import com.yiren.core.utils.UUIDGenerator;
import com.yiren.entity.YrElderTestAnswerRecord;
import com.yiren.entity.YrElderTestResultRecord;
import com.yiren.entity.YrTestResultItem;
import com.yiren.entity.YrTestTopicItemAnswer;
import com.yiren.manager.ctrl.GenericController;
import com.yiren.manager.form.test.YrElderTestAnswerRecordForm;
import com.yiren.manager.form.test.YrElderTestResultRecordForm;
import com.yiren.manager.service.iface.test.YrElderTestAnswerRecordService;
import com.yiren.manager.service.iface.test.YrElderTestResultRecordService;
import com.yiren.manager.service.iface.test.YrTestResultItemService;
import com.yiren.manager.service.iface.test.YrTestTopicItemAnswerService;
import com.yiren.manager.utils.ObjectUtils;

@RequestMapping("elderTestAnswerRecord")
@Controller
public class YrElderTestAnswerRecordController extends GenericController<YrElderTestAnswerRecordForm, YrElderTestAnswerRecord>{

	@Autowired
	private YrElderTestAnswerRecordService selfService;
	
	@Autowired
	private YrTestTopicItemAnswerService testTopicItemAnswerService;
	
	@Autowired
	private YrTestResultItemService testResultItemService;
	
	@Autowired
	private YrElderTestResultRecordService elderTestResultRecordService;
	
	@RequestMapping("saveBatch")
	@ResponseBody
	public HTTPResult<YrElderTestAnswerRecordForm> saveBatch(String data) throws SwallowException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, MyException{
		HTTPResult<YrElderTestAnswerRecordForm> result = new HTTPResult<YrElderTestAnswerRecordForm>();
		JsonMapperTool tool = new JsonMapperTool(data);
		System.out.println(data.toString());
		ArrayList<YrElderTestAnswerRecordForm> elderTestAnswerRecordForms = tool.mapper("elderTestAnswerRecords",
			new TypeReference<ArrayList<YrElderTestAnswerRecordForm>>() {});
		
		YrElderTestResultRecordForm testResultRecordForm = tool.mapper("testResultRecord", YrElderTestResultRecordForm.class);
		if(null==elderTestAnswerRecordForms||null==testResultRecordForm){
			throwExp("参数有误");
		}
		String sqlWhere = "elder_id=?1 AND test_topic_id=?2 AND test_times_number=?3";
		QueryResult<YrElderTestResultRecord> qr = elderTestResultRecordService.find(YrElderTestResultRecord.class, sqlWhere, new Object[]{testResultRecordForm.getElderId(),testResultRecordForm.getTestTopicId(),testResultRecordForm.getTestTimesNumber()});
		if(qr.getTotalRecord()>0){
			throwExp("该老者已测试过该题目，请勿重复测试");
		}
		
		Date now = new Date();
		Float score = Float.valueOf(0);
		List<YrElderTestAnswerRecord> entityList = new ArrayList<YrElderTestAnswerRecord>();
		for(YrElderTestAnswerRecordForm elderTestAnswerRecordForm : elderTestAnswerRecordForms){
			YrElderTestAnswerRecord entity = ObjectUtils.objectToObject(elderTestAnswerRecordForm, YrElderTestAnswerRecord.class);
			entity.setAnswerRecordId(UUIDGenerator.getUUID());
			entity.setCreateTime(now);
			YrTestTopicItemAnswer testTopicItemAnswer = testTopicItemAnswerService.findOne(entity.getAnswerId());
			if(null==testTopicItemAnswer){
				throwExp("保存出错：答案列表查询出错");
			}
			score = score + testTopicItemAnswer.getAnswerScore();
			entityList.add(entity);
		}
		
		sqlWhere = "score_min<=?1 AND score_max>=?2 AND test_topice_id=?3";	
		QueryResult<YrTestResultItem> qrTestResultItem = testResultItemService.find(YrTestResultItem.class, sqlWhere, new Object[]{score,score,testResultRecordForm.getTestTopicId()});
		
		if(qrTestResultItem.getTotalRecord()==0){
			throwExp("查询不到测试结果记录");
		}
		
		YrTestResultItem testResultItem = qrTestResultItem.getResultList().get(0);
		YrElderTestResultRecord elderTestResultRecord = ObjectUtils.objectToObject(testResultRecordForm, YrElderTestResultRecord.class);
		elderTestResultRecord.setResultRecordId(UUIDGenerator.getUUID());
		elderTestResultRecord.setTestResultItemId(testResultItem.getResultItemId());
		elderTestResultRecord.setScore(score);
		elderTestResultRecord.setCreateTime(now);
		
		FavoriteSet favoriteSet = new FavoriteSet();
		favoriteSet.putFavorite("elderTestAnswerRecordList", entityList);
		favoriteSet.putFavorite("testResultRecord", elderTestResultRecord);
		
		selfService.saveBatch(favoriteSet);
		
		return result;
	}
	
	@Override
	public YrElderTestAnswerRecordForm toForm(YrElderTestAnswerRecord e) throws Exception {
		YrElderTestAnswerRecordForm form = ObjectUtils.objectToObject(e, YrElderTestAnswerRecordForm.class);
		return form;
	}

	@Override
	public YrElderTestAnswerRecord toEntity(YrElderTestAnswerRecordForm f) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

}
